const db = require('../models/db.js')
const moment = require('moment')

exports.addComment = async (body) => {
	try {
		const curTime = moment().format('YYYY-MM-DD hh:mm:ss')
		const {
			content = '',
			topic_id,
			user_id
		} = body
		const strSql = `
			insert into comments (content,create_time,modify_time,topic_id,user_id)
			values (
				'${content}',
				'${curTime}',
				'${curTime}',
				'${topic_id}',
				'${user_id}'
			)
		`
		const ret = await db.query(strSql)
		return ret.insertId
	} catch(err) {
		throw err
	}
}

exports.getCommentById = async (id) => {
	try {
		const strSql = `
			select * from comments where id='${id}'
		`
		const [comment] = await db.query(strSql)
		return comment
	} catch(err) {
		throw err
	}
}

exports.getCommentsByTopicId = async (topic_id) => {
	try {
		const strSql = `
			select * from comments where topic_id=${topic_id} order by id desc
		`
		const comments = await db.query(strSql)
		return comments
	} catch(err) {
		throw err
	}
}